﻿using Prism.Commands;
using StarryEdge.Core;
using StarryEdge.Core.Parameter;
using StarryEdge.Menu.Device.Helper;
using StarryEdge.Menu.Device.Model;
using StarryEdge.Parameter;
using StarryEdge.WPF.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace StarryEdge.Menu.Device.ViewModels
{
    public class NicConfigurationViewModel
    {
        public IPPortModel IPPort { get; set; }
        private List<NetworkInterface> _networkInterfaces = new List<NetworkInterface>();
        public ObservableCollection<string> NICCList { get; set; }
        public ObservableCollection<IPPortModel> IPPortList { get; set; }

        private IStarryEdgeLog _log;
        private IDialogHost _dialogHost;
        private IUserEntity _userEntity;

        public NicConfigurationViewModel(IStarryEdgeLog log, IDialogHost dialogHost, IUserEntity userEntity)
        {
            _userEntity = userEntity;
            _log = log;
            _dialogHost = dialogHost;
            _networkInterfaces.AddRange(NetworkAdapterHelper.GetAllNetworkAdapters());
            NICCList = new ObservableCollection<string>(_networkInterfaces.Select(x => x.Name).ToList());
            IPPortList = new ObservableCollection<IPPortModel>(new IPPortModel[] {
                new IPPortModel(){
                    IP = "192.168.1.200",
                    DNS = "255.255.255.0",
                },
                new IPPortModel(){
                    IP = "192.168.11.200",
                    DNS = "255.255.255.0",
                },
                new IPPortModel(){
                    IP = "192.168.16.200",
                    DNS = "255.255.255.0",
                },
                new IPPortModel(){
                    IP = "192.168.100.200",
                    DNS = "255.255.255.0",
                },
                new IPPortModel(){
                    IP = "192.168.250.200",
                    DNS = "255.255.255.0",
                }
            });
            IPPort = new IPPortModel()
            {
                IP = "192.168.10.200",
                DNS = "255.255.255.0",
            };
            if (_networkInterfaces.Count > 0)
            {
                NICC = _networkInterfaces[0].Name;
            }
        }

        private string _nicc;
        public string NICC
        {
            get { return _nicc; }
            set
            {
                _nicc = value;
            }
        }

        public DelegateCommand Update => new DelegateCommand(() =>
        {
            _networkInterfaces.Clear();
            NICCList.Clear();
            _networkInterfaces.AddRange(NetworkAdapterHelper.GetAllNetworkAdapters());
            foreach (var item in _networkInterfaces)
            {
                NICCList.Add(item.Name);
            }
            if (_networkInterfaces.Count > 0)
            {
                NICC = _networkInterfaces[0].Name;
            }
        });

        public DelegateCommand Setting => new DelegateCommand(() =>
        {
            var model = _networkInterfaces.FirstOrDefault(x => x.Name == NICC);
            if (model == null) { _dialogHost.DialogShow("找不到网卡", Core.Enums.LogLevel.Warning); return; }
            if (NetworkAdapterHelper.SetIPAddress(IPPortList.Select(x => x.IP).ToArray(), IPPortList.Select(x => x.DNS).ToArray(), model.Id))
            {
                _log.LogControls($"设置成功--[{System.Text.Json.JsonSerializer.Serialize(IPPortList)}]", _userEntity.GetLoginUser()?.Name, Core.Enums.LogLevel.Success);
                _dialogHost.DialogShow("设置成功", Core.Enums.LogLevel.Success);
            }
            else
            {
                _log.LogControls("设置失败", _userEntity.GetLoginUser()?.Name, Core.Enums.LogLevel.Warning);
                _dialogHost.DialogShow("设置失败", Core.Enums.LogLevel.Warning);
            }
        });

        public DelegateCommand Add => new DelegateCommand(() =>
        {
            if (IPPortList.Any(x => x.IP == IPPort.IP && x.DNS == IPPort.DNS))
            {
                return;
            }
            IPPortList.Add(new IPPortModel()
            {
                IP = IPPort.IP,
                DNS = IPPort.DNS,
            });
        });
        public DelegateCommand Delete => new DelegateCommand(() =>
        {
            var model = IPPortList.FirstOrDefault(x => x.IP == IPPort.IP && x.DNS == IPPort.DNS);
            if (model != null)
            {
                IPPortList.Remove(model);
            }
        });

        public DelegateCommand<IPPortModel> MouseDoubleClick => new DelegateCommand<IPPortModel>(sender =>
        {
            if (sender != null)
            {
                IPPort.IP = sender.IP;
                IPPort.DNS = sender.DNS;
            }
        });
    }
}
